package com.ghxxzx.daglcustom.business_form.service;

import com.ghxxzx.daglcustom.business_form.domain.FormPo;
import com.ghxxzx.daglcustom.business_form.domain.FormTypePo;
import com.ghxxzx.daglcustom.business_form.domain.FormValsPo;
import com.ghxxzx.daglcustom.business_form.domain.dto.FormSubmitDto;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tmsps.ne4spring.base.BaseService;
import org.tmsps.ne4spring.utils.ChkUtil;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;

@Service
public class FormValsService extends BaseService {

	@Autowired
	private TaskService taskService;
	@Autowired
	private Form2Service formService;

	@Transactional
	public void saveFormVals(@Valid FormSubmitDto formSubmitDto) {
		String task_id = formSubmitDto.getTask_id();
		Task task = taskService.createTaskQuery().taskId(task_id).singleResult();

		// 1. 获取表单参数
		String form_key = task.getFormKey();
		FormPo formPo = formService.getFormByFormKey(form_key);
		FormTypePo formTypePo = bs.findById(formPo.getType_kid(), FormTypePo.class);
		// 2.创建表单值
		// 检测是否草稿中
		FormValsPo formValsPo = this.getFormValByTaskId(task_id);
		if (formValsPo == null) {
			formValsPo = new FormValsPo();
		}
		formValsPo.setForm_type_kid(formTypePo.getKid());
		formValsPo.setForm_type_name(formTypePo.getType());
		formValsPo.setForm_kid(formPo.getKid());

		// 3.绑定流程
		formValsPo.setAssigner(task.getAssignee());// 赋值操作人 : 改为当前登录用户
		formValsPo.setActi_task_id(task.getId());
		formValsPo.setActi_proc_inst_id(task.getProcessInstanceId());

		// 4.是否已经有表单流转
		FormValsPo firstFormVals = this.getFirstForm(task.getProcessInstanceId());
		// 如上,按照审批的过程,加排序
		int sortz = this.getFormValsCount(task.getProcessInstanceId());
		formValsPo.setSortz(sortz);

		// 表单json值: 快照
		formValsPo.setForm_json(formPo.getJson());
		// 存储表单值json值:填写的表单值
		formValsPo.setForm_vals_json(formSubmitDto.getForm());

		// 表单类型:填写单 or 审批单 or 流转单
		if (firstFormVals == null || firstFormVals.getForm_kid().equals(formValsPo.getForm_kid())) {
			formValsPo.setType("填写单");
		} else {
			formValsPo.setType("审批单");
		}

		// 如果是流转单, 承接的上个id; 比如: 请假单-->销假单
//		formValsPo.setRefer_kid(refer_kid);// 预留接口,目前不知道怎么写

		// 表单状态: 0 正常 or -100 作废
		if (firstFormVals != null && firstFormVals.getForm_kid().equals(formValsPo.getForm_kid())) {
			// 表单:知道同一类表单,有利于表单重置:statuz为 -100
			firstFormVals.setStatuz(-100);
			bs.updateObj(firstFormVals);
		}

		formValsPo.setStatuz(0);
		if (ChkUtil.isNull(formValsPo.getKid())) {
			bs.saveObj(formValsPo);
		} else {
			bs.updateObj(formValsPo);
		}
	}

	/**
	 * .查询第一个表单
	 * 
	 * @param processInstanceId
	 * @return
	 */
	public FormValsPo getFirstForm(String processInstanceId) {
		String sql = "select * from t_form_vals t where t.status=0 and t.sortz=0 and t.acti_proc_inst_id=?";
		FormValsPo formValsPo = bs.findObj(FormValsPo.class, sql, processInstanceId);
		return formValsPo;
	}

	public int getFormValsCount(String processInstanceId) {
		String sql = "select count(*) cnt from t_form_vals t where t.status=0 and t.acti_proc_inst_id=?";
		Number cnt = (Number) jt.queryForMap(sql, processInstanceId).get("cnt");
		return cnt.intValue();
	}

	// 查询上一次表单
	public FormValsPo getPreFormVals(String processInstanceId, String form_kid) {
		String sql = "select * from t_form_vals t where t.status=0 and t.type='填写单' and t.acti_proc_inst_id=? and t.form_kid=? order by t.sortz desc";
		List<FormValsPo> list = bs.findList(FormValsPo.class, sql, processInstanceId, form_kid);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	// 查询之前填写过的表单
	public List<FormValsPo> getAllBeforForms(String processInstanceId, int sortz) {
		String sql = "select * from t_form_vals t where t.status=0 and t.acti_proc_inst_id=? and t.sortz<? order by t.sortz asc";
		List<FormValsPo> list = bs.findList(FormValsPo.class, sql, processInstanceId, sortz);
		return list;
	}

	public List<FormValsPo> getAllBeforForms(String processInstanceId, String task_id) {
		String sql = "select * from t_form_vals t where t.status=0 and t.acti_proc_inst_id=? and t.acti_task_id!=? order by t.sortz asc";
		List<FormValsPo> list = bs.findList(FormValsPo.class, sql, processInstanceId, task_id);
		return list;
	}

	// 根据task_id 获取表单
	public FormValsPo getFormValByTaskId(String task_id) {
		String sql = "select * from t_form_vals t where t.status=0 and t.acti_task_id=? ";
		FormValsPo formValsPo = bs.findObj(FormValsPo.class, sql, task_id);
		return formValsPo;
	}

}
